{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Plot a 13th-order filter’s impulse response using both lfilter and sosfilt, showing the instability that results from trying to do a 13th-order filter in a single stage (the numerical error pushes some poles outside of the unit circle):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "from scipy import signal\n",
    "b, a = signal.ellip(13, 0.009, 80, 0.05, output='ba')\n",
    "sos = signal.ellip(13, 0.009, 80, 0.05, output='sos')\n",
    "z, p, k = signal.tf2zpk(b, a)\n",
    "print('len(z) = ', len(z))\n",
    "print('len(p) = ', len(p))\n",
    "x = signal.unit_impulse(700)\n",
    "y_tf = signal.lfilter(b, a, x)\n",
    "y_sos = signal.sosfilt(sos, x)\n",
    "plt.figure(1, dpi=800)\n",
    "plt.plot(y_tf, 'r', label='TF', linewidth=0.5)\n",
    "plt.plot(y_sos, 'k', label='SOS', linewidth=0.5)\n",
    "plt.legend(loc='best')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
